package com.tom.adapter.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.tom.mapper.AnalysisSqlMapper;
import com.tom.model.req.AnalysisSqlReq;
import com.tom.model.req.ExecuteSqlReq;
import com.tom.adapter.AnalysisSqlService;
import com.tom.utils.AnalysisScriptSqlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author zx
 * @date 2024-11-09 18:07
 */
@Service
public class AnalysisSqlServiceImpl implements AnalysisSqlService {

    @Autowired
    private AnalysisSqlMapper analysisSqlMapper;
    @Override
    public String analysisSql(AnalysisSqlReq analysisSqlReq) {
        String scriptSql = analysisSqlReq.getScriptSql();
        JSONObject paramsJsonObject = analysisSqlReq.getParams();
        Map<String,Object> params = JSONUtil.toBean(paramsJsonObject, Map.class);
        return AnalysisScriptSqlUtils.analysisDynamicScriptSql(scriptSql, params);
    }

    @Override
    public List<Map<String, Object>> executeSql(ExecuteSqlReq executeSqlReq) {
        return analysisSqlMapper.executeDynamicSql(executeSqlReq.getSql());
    }
}
